package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Statement;
import java.util.List;

public class UserContext {
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION="EXPRESSION_IN_SESSION";
    public static final String UNREADNUM_IN_SESSION="UNREADNUM_IN_SESSION";
    public static final String UNREAD_IN_SESSION="UNREAD_IN_SESSION";

    //利用springmvc提供的工具类 获取session
    public static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }
    //从session中获取登陆用户
    public static Employee getContextUser(){
        return (Employee) getSession().getAttribute("USER_IN_SESSION");
    }

    //把用户存到session中
    public static void setContextUser(Employee employee){
        getSession().setAttribute("USER_IN_SESSION",employee);
    }

    //把权限表达式集合存到session中
    public static void  setExpression(List<String> list){
        getSession().setAttribute("EXPRESSION_IN_SESSION",list);
    }

    //从session中获取权限表达式
    public static List<String> getExpression(){
        return (List<String>)getSession().getAttribute("EXPRESSION_IN_SESSION");
    }
    //把未读数量存到session中
    public static void setUnreadnumInSession(Integer num){
        getSession().setAttribute("UNREADNUM_IN_SESSION",num);
    }
    //把未读数量从session中取出来
    public static Integer getUnreadnumInSession(){
        return (int)getSession().getAttribute("UNREADNUM_IN_SESSION");
    }
    //把未读集合存到session中
    public static void setUnreadInSession(List<Notice> list){
        getSession().setAttribute("UNREAD_IN_SESSION",list);
    }
    //把未读集合从session中取出来
    public static List<Notice> getUnreadInSession(){
        return (List<Notice>)getSession().getAttribute("UNREAD_IN_SESSION");
    }
}
